package edu.zut.cs.software.example.Register.web.spring.controller;

/*
 * @author 裴萌洁
 */
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import edu.zut.cs.software.example.student.domain.Student;
import edu.zut.cs.software.example.student.service.StudentManager;
import edu.zut.cs.software.example.util.JWT.JwtHelper;

@RestController
public class RegisterController {
	@Autowired
	StudentManager studentManager;

	@RequestMapping("/register")
	public String register(@RequestBody Student student,HttpServletRequest request) {
		System.out.println(" 访问成功");
//		String token = null;
		String flag ="";
		
//		String ACtoken = request.getHeader("Authorization");
		
//		if(ACtoken.length()==4) {
			
			if(studentManager.findByuserId(student.getUserId()))
			{
				//用户名重复
				flag="error";
			}
			else
			{
				studentManager.adduser(student);
//				token=JwtHelper.createToken(student.getUserId());
			}
//		}

		
		HashMap<String,Object> res=new HashMap<String, Object>();//放入map集合K:V
		res.put("flag", flag);//收容标记flag 前是字符串后是字符串对象定义的hashmap
//		res.put("token",token);
		String res_json=JSON.toJSONString(res);//把一个对象转换为json串:把收容有flag标记和student对象转变成json数据

		return res_json;//含有两块信息回应给前端
//		return null;
	}
}

